package me.xingyannian.zhaopin.service;

import me.xingyannian.zhaopin.entity.ChangCi;
import me.xingyannian.zhaopin.entity.Job;
import me.xingyannian.zhaopin.repository.JobRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class JobService {
    private final JobRepository jobRepository;

    public JobService(JobRepository jobRepository) {
        this.jobRepository = jobRepository;
    }

    // 分页查询
    public Page<Job> findAll(int page, int size) {
        Pageable pageable = Pageable.ofSize(size).withPage(page);
        return this.jobRepository.findAll(pageable);
    }
    //
    public Page<Job> findAllByChangCi(ChangCi changCi, int page, int size) {
        Pageable pageable = Pageable.ofSize(size).withPage(page);
        return this.jobRepository.findAllByChangCi(changCi ,pageable);
    }
    // 批量导入保存,开启事务
    @Transactional
    public void saveAll(List<Job> jobs) {
        this.jobRepository.saveAll(jobs);
    }
    // 根据场次查询学科数据
    public List<String> findDistinctSubjectByChangCi(ChangCi changCi) {
        return this.jobRepository.findDistinctSubjectByChangCi(changCi);
    }
    // 根据场次、学科、学段查询统计数据
    public Integer countByChangCiAndSubjectAndStage(ChangCi changCi, String subject, String stage) {
        var jobs = this.jobRepository.findAllByChangCiAndSubjectAndStage(changCi, subject, stage);
        Integer count = 0;
        for (Job job : jobs) {
            count += job.getCount();
        }
        return count;
    }

    public List<String> findDistinctRegionByChangCi(ChangCi changCi) {
        return this.jobRepository.findDistinctRegionByChangCi(changCi);
    }

    public Integer countByChangCiAndRegionAndDegree(String degree, ChangCi changCi, String region) {
        var jobs = this.jobRepository.findAllByChangCiAndRegionAndDegree(changCi, region, degree);
        Integer count = 0;
        for (Job job : jobs) {
            count += job.getCount();
        }
        return count;
    }
}
